package lcy.homework05.staticTest;

import java.util.Arrays;

/**
 * @ClassName: Sorting
 * @Description:
 * @author: 李春洋
 * @date: 2024/5/13 16:14
 * @Blog: https://
 */
public class Sorting {
    // 使Double数组按从大到小顺序排序
    public static void sortDescending(Double[] array) {
        Arrays.sort(array, (a, b) -> Double.compare(b, a));
    }

    // 使Double数组按从小到大排序
    public static void sortAscending(Double[] array) {
        Arrays.sort(array);
    }

    // 使Double数组反序排序
    public static void reverseSort(Double[] array) {
        Arrays.sort(array, (a, b) -> Double.compare(b, a));
    }

    // 计算阶乘的递归方法
    public static int factorial(int n) {
        if (n == 0) {
            return 1;
        } else {
            return n * factorial(n - 1);
        }
    }

    public static void main(String[] args) {
        // 测试排序方法
        Double[] numbers = {3.2, 1.5, 4.8, 2.1, 5.6};
        System.out.println("开始的顺序: " + Arrays.toString(numbers));

        // 测试按从大到小顺序排序
        sortDescending(numbers);
        System.out.println("降序: " + Arrays.toString(numbers));

        // 测试按从小到大排序
        sortAscending(numbers);
        System.out.println("升序: " + Arrays.toString(numbers));

        // 测试反序排序
        reverseSort(numbers);
        System.out.println("反序排序: " + Arrays.toString(numbers));

        // 测试阶乘方法
        System.out.println("5的阶乘: " + factorial(5));
    }
}
